<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Controllers\OATrait\OATrait;
use App\Http\Requests\NoteRequest;
use App\Http\Resources\NoteResource;
use App\Model\OANote;
use Illuminate\Http\Request;


class NoteController extends Controller
{
    use OATrait;
    public function index(Request $request)
    {
       $params = $this->mergeRequest($request);
        $list = OANote::query()->with('files')
                      ->where('uid',$params['uid'])
                      ->when($request->filled('pid') && $request->pid,function ($q) use ($request){
                          return $q->where('pid',$request->pid);
                      })
                    ->when($request->filled('type') && $request->type,function ($q) use ($request){
                        return $q->where('type',$request->type);
                    })
                    ->when($request->filled('roleid') && $request->roleid,function ($q) use ($request){
                        return $q->where('roleid',$request->roleid);
                    })
                    ->when($request->filled('title') && $request->title,function ($q) use ($request){
                        return $q->where('title','like',$request->title . '%');
                    })
                   ->paginate($request->limit ?? 10);
        return $this->SucRes(NoteResource::collection($list));

    }

    //新增
    public function store(NoteRequest $request)
    {
        $param = $this->mergeRequest($request);
        $note = OANote::query()->create($param);
        $files = [];
        if($request->filled('fileList') && !empty($request->fileList)){
            array_map(function ($file) use (&$files,$note){
                $files[] = [
                    'pid'=>$note->id,
                    'title'=>pathinfo($file['fileName'],PATHINFO_FILENAME),
                    'fsize'=>$file['size'],
                    'hz'=>$file['ext'],
                    'furl'=>$file['fileUrl'],
                    'fdir'=>'note',
                    'uid'=>$note->uid,
                ];
            },$request->fileList);
        }
      if($files) $note->files()->createMany($files);
      return $this->SucRes(['id'=>$note->id]);
    }

    //修改
    public function update(NoteRequest $request,OANote $note)
    {
       $params = $this->mergeRequest($request);
       if($params['uid'] != $note->uid) throw new \Exception('仅可修改自己的备注');
        $note->title = $params['title'];
        $note->note = $params['note'];
        $note->save();
        $files = [];
        if($request->filled('fileList') && !empty($request->fileList)){
            array_map(function ($file) use (&$files,$note){
                $files[] = [
                    'pid'=>$note->id,
                    'title'=>pathinfo($file['fileName'],PATHINFO_FILENAME),
                    'fsize'=>$file['size'],
                    'hz'=>$file['ext'],
                    'furl'=>$file['fileUrl'],
                    'fdir'=>'note',
                    'uid'=>$note->uid,
                ];
            },$request->fileList);
        }

        if($files) {
            $note->files()->createMany($files);
        }
        return $this->SucRes(['id'=>$note->id]);
    }
    //删除
    public function destroy(OANote $note)
    {
        $note->delete();
        $note->files()->delete();
        return $this->SucRes();
    }

    public function show(OANote $note)
    {
        return $this->SucRes(NoteResource::make($note));
    }

}
